<template>
	<jk-dialog
		v-model="showDialog"
		:title="`选择-${title}`"
		width="1000px"
		append-to-body
		@on-visible-change="onVisibleChange"
		@on-cancel="onVisibleChange(false)"
		@on-confirm="onConfirmBtn"
	>
		<header ref="box1" class="margin-bottom-10 flex-end-center">
			<jk-search-input v-model="searchBarNameOrCode" show-search class="search-bar-item-size3" placeholder="编号或名称" @on-search="onSearchBtn" @keyup.enter.native="onSearchBtn"></jk-search-input>
		</header>

		<jk-table
			ref="jkTable"
			:radio-config="{trigger: 'row'}"
			:loading="tableLoading"
			class="width-100-percent"
			:data="tableData"
			border
			highlight-hover-row
			highlight-current-row
			:height="tableHeight - 100"
			@radio-change="onRadioChange"
			@cell-dblclick="onCellDblclick"
		>
			<vxe-table-column type="radio" width="60" align="center" title="单选" fixed="left"></vxe-table-column>
			<vxe-table-column field="name" title="姓名" fixed :width="100"> </vxe-table-column>
			<vxe-table-column field="code" title="编号" sortable :width="100" />
			<vxe-table-column field="gender" title="性别" align="center" :width="90">
				<template #default="scope">
					<el-tag :type="scope.row.gender ? 'success' : 'info'" size="mini">{{ scope.row.gender ? '男' : '女' }}</el-tag>
				</template>
			</vxe-table-column>
			<vxe-table-column field="groupName" title="班组" min-width="80" />
			<vxe-table-column field="leaderName" title="直接上级" min-width="90" />
			<vxe-table-column field="deptName" title="所属部门" min-width="100" />
			<vxe-table-column field="postName" title="岗位" min-width="100" />
		</jk-table>
		<footer ref="box2" class="flex-end-center margin-top-10">
			<jk-page
				:page-index="pageNo"
				:page-size="pageSize"
				layout="total, prev, pager, next"
				:total="totalCount"
				@on-change="onPageChange"
			/>
		</footer>
	</jk-dialog>
</template>

<script>
    import calDialogHeight from '../../../../../mixins/cal-table-height';
    import { pageConfig } from '../../../../../utils/jk/jk-tool';
    import { getEmpPage } from '../../../../../api/system/emp';

    export default {
        name: 'SelectProductionDialog',
        mixins: [calDialogHeight],
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            title: {
                type: String,
                default: '人员'
            }
        },
        data() {
            return {
                selectRow: null,
                searchBarProcessId: null,
                searchBarNameOrCode: '',
                pageNo: 1,
                pageSize: pageConfig.pageSize,
                totalCount: null,
                tableData: [],
                showDialog: false,
                tableLoading: false,
                selectData: []
            };
        },
        methods: {
            onCellDblclick(e) {
                this.$emit('on-confirm', e.row);
            },
            onRadioChange(selection) {
                this.selectData = selection.row;
            },
            checkboxMethod({ row }) {
                return row._disabled !== true;
            },
            onSearchBtn() {
                this.pageNo = 1;
                this.getListRequest();
            },
            onPageChange(e) {
                this.pageNo = e;
                this.getListRequest();
            },
            onConfirmBtn() {
                if (Object.keys(this.selectData).length) {
                    this.$emit('on-confirm', this.selectData);
                } else {
                    this.$tipsBar(this, 'unChoose');
                }
            },
            getListRequest() {
                this.tableLoading = true;
                this.selectData = [];
                return getEmpPage({
                    pageNo: this.pageNo,
                    pageSize: this.pageSize,
                    billNo: this.searchBarNameOrCode,
                    status: 1
                }).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.totalCount = res.data.count;
                        this.tableData = res.data.res;
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                }
            },
            async getDependentData() {
                this.tableLoading = true;
                await this.getListRequest();
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };
</script>
